package com.csxy.m2020_note;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.csxy.m2020_note.sql.NoteDbHelper;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    Button btnAdd;
    ListView listView;
    List<Notebean> notebeanList;
    NotesAdapter notesAdapter;
    NoteDbHelper noteDbHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        noteDbHelper=new NoteDbHelper(MainActivity.this);
        noteDbHelper.open();


    }

    //刷新界面
    @Override
    protected void onResume() {
        super.onResume();
        initView();
    }

    void initView(){
        listView=findViewById(R.id.listview);

        //notebeanList = new ArrayList<Notebean>();
        notebeanList =noteDbHelper.getAllNotes();
        //数据不能是模拟，需要真正的从数据库中查出来
       /* for(int i=0;i<10;i++)
        {
            Notebean m=new Notebean();
            m.setId(i);
            m.setBody(i+"1111111111111111");
            m.setTitle(i+"222222222222");
            notebeanList.add(m);
        }*/

        notesAdapter=new NotesAdapter(MainActivity.this,R.layout.note_item,notebeanList);
        listView.setAdapter(notesAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                   Notebean notebean=notebeanList.get(position);
                Toast.makeText(MainActivity.this,notebean.getTitle()+
                        ":"+notebean.getBody(),Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(MainActivity.this,EditActivity.class);
                intent.putExtra("title",notebean.getTitle());
                intent.putExtra("id",notebean.getId());
                intent.putExtra("body",notebean.getBody());
                startActivity(intent);


            }
        });

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                showDeleteDialog(position);
                //return false;会触发setOnItemClickListener事件
                return true;//不会触发单击事件  相当于触摸事件至此为止
            }
        });

    btnAdd=findViewById(R.id.btn_add);
    btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            createNote();
        }
    });
    }

    void createNote(){
        Intent intent=new Intent(MainActivity.this,NewActivity.class);
        startActivity(intent);
    }


    private void showDeleteDialog(final int position){
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setIcon(R.mipmap.ic_launcher)//设置标题的图片
                .setTitle("确认")//设置对话框的标题
                .setMessage("确认要删除笔记吗")//设置对话框的内容
                //设置对话框的按钮
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "点击了取消按钮", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();//关闭窗口
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {


                        noteDbHelper.deleteNote(notebeanList.get(position).getId());
                        notebeanList.remove(position);
                        notesAdapter.notifyDataSetChanged();
                        Toast.makeText(MainActivity.this, "笔记已删除", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                }).create();
        dialog.show();//显示对话框
    }

}
